﻿using System;
using System.Collections.Generic;

namespace Longbow.Cache
{
    /// <summary>
    /// 缓存接口定义
    /// </summary>
    public interface ICache
    {
        /// <summary>
        /// 获取或者添加方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="valueFactory"></param>
        /// <param name="profileKey"></param>
        /// <returns></returns>
        T GetOrAdd<T>(string key, Func<string, T> valueFactory, string profileKey = null);

        /// <summary>
        /// 获取或者添加方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="option"></param>
        /// <param name="valueFactory"></param>
        /// <returns></returns>
        T GetOrAdd<T>(ICacheOption option, Func<string, T> valueFactory);

        /// <summary>
        /// 添加或者更新方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="valueFactory"></param>
        /// <param name="updateFactory"></param>
        /// <param name="profileKey"></param>
        /// <returns></returns>
        T AddOrUpdate<T>(string key, Func<string, T> valueFactory, Func<string, T, T> updateFactory, string profileKey = null);

        /// <summary>
        /// 添加或者更新方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="option"></param>
        /// <param name="valueFactory"></param>
        /// <param name="updateFactory"></param>
        /// <returns></returns>
        T AddOrUpdate<T>(ICacheOption option, Func<string, T> valueFactory, Func<string, T, T> updateFactory);

        /// <summary>
        /// 获取方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        T Get<T>(string key);

        /// <summary>
        /// 清除方法
        /// </summary>
        void Clear(IEnumerable<string> keys);

        /// <summary>
        /// 清除方法
        /// </summary>
        /// <param name="key">结尾支持通配符*</param>
        void Clear(string key = null);

        /// <summary>
        /// 获取缓存总数
        /// </summary>
        /// <returns></returns>
        int Count { get; }

        /// <summary>
        /// ToList 变换
        /// </summary>
        /// <returns></returns>
        IEnumerable<ICacheItem> ToList();

        /// <summary>
        /// 获得 Keys 集合
        /// </summary>
        IEnumerable<string> Keys { get; }

        /// <summary>
        /// 跨站点缓存清除方法
        /// </summary>
        /// <param name="keys"></param>
        void CorsClear(IEnumerable<string> keys);

        /// <summary>
        /// 获得 跨站点配置项集合
        /// </summary>
        IEnumerable<ICacheCorsItem> CorsSites { get; }
    }
}
